package com.doudou.collection.controller;

import com.doudou.collection.pojo.dto.AnnouncementAddNewDTO;
import com.doudou.collection.response.ResultVO;
import com.doudou.collection.security.CustomUserDetails;
import com.doudou.collection.service.AnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags="05.公告模块")
@Slf4j
@RestController
@RequestMapping("/v1/announcements")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @ApiOperation("发布公告")
    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping("/add-new")
    public ResultVO addNew(AnnouncementAddNewDTO announcementAddNewDTO,
                           @AuthenticationPrincipal CustomUserDetails userDetails) {
        log.debug("开始处理【发布公告】请求: {}", announcementAddNewDTO);

        announcementService.addNew(announcementAddNewDTO, userDetails);
        log.info("发布成功");
        return ResultVO.ok();
    }

    @ApiOperation("查询公告")
    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping("/select")
    public ResultVO selectAnnouncementAll() {
        log.debug("开始处理【查询所有公告】请求");

        return announcementService.selectAll();
    }

    @ApiOperation("修改公告")
    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping("/{id}/update")
    public ResultVO editAnnouncement(@PathVariable Long id) {
        log.debug("开始处理【修改公告】请求: {}", id);

        return announcementService.editAnnouncementById(id);
    }


    @ApiOperation("修改公告撤回状态")
    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping("/{id}/{ifCancel}/changeIfCancel")
    public ResultVO changeIfCancel(@PathVariable Long id, @PathVariable Integer ifCancel) {
        log.debug("开始处理【修改公告撤回状态】请求：公告ID:{}, 撤回状态:{}", id, ifCancel);

        return announcementService.updateIfCancel(id, ifCancel);

    }

    @ApiOperation("修改公告删除状态")
    @PreAuthorize("hasAuthority('ADMIN')")
    @PostMapping("/{id}/{delFlag}/changeDelFlag")
    public ResultVO changeDelFlag(@PathVariable Long id, @PathVariable Integer delFlag) {
        log.debug("开始处理【修改公告删除状态】请求：公告ID:{}, 删除状态:{}", id, delFlag);

        return announcementService.updateDelFlag(id, delFlag);

    }


}
